package yf.mall.controller.api;

import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.entity.AccCardEntity;
import yf.mall.service.AccCardService;

import java.util.HashMap;
import java.util.List;

/**
 * Long
 */
@RequestMapping("/api/accCard")
@RestController
public class ApiAccCardController {

    @Autowired
    private AccCardService accCardService;


    /**
     * Long
     * 查询所有的银行卡信息
     * @param userId
     * @return
     */
    @RequestMapping("accList")
    public R accList(@LoginUserId long userId){
        List<AccCardEntity> accCardEntities = this.accCardService.accList(userId);
        return R.ok(accCardEntities);
    }

    /**
     * Long
     * 保存银行卡
     * @param userId
     * @param accCardEntity
     * @return
     */
    @RequestMapping("save")
    public R saveAcc(@LoginUserId Long userId, AccCardEntity accCardEntity){
        accCardEntity.setUserId(userId);
        HashMap<String, Object> params = new HashMap<>();
        params.put("number",accCardEntity.getNumber());
        List<AccCardEntity> accCardEntities = this.accCardService.queryList(params);
        if (accCardEntities!=null&&accCardEntities.size()>0){
            throw new RRException("该银行卡已在系统中");
        }
        this.accCardService.save(accCardEntity);
        return R.ok();
    }

    @RequestMapping("update")
    public R updateAcc(AccCardEntity accCardEntity){
        this.accCardService.update(accCardEntity);
        return R.ok();
    }

    @RequestMapping("delete")
    public R deleteAcc(@LoginUserId long userId , Long id){
        this.accCardService.deleteByUserId(userId,id);
        return R.ok();
    }
}
